import { Disposable, ICommandService, Inject } from '@univerjs/core';
import { ComponentManager, IMenuManagerService, RibbonStartGroup } from '@univerjs/ui';
import { ChartSingle, LineChartSingle, PieChartSingle, RadarChartSingle, SystemSingle } from '@univerjs/icons';

import {
  JnpfSheetsInsertedEchartOperation,
  JnpfSheetsInsertBarEchartOperation,
  JnpfSheetsInsertLineEchartOperation,
  JnpfSheetsInsertPieEchartOperation,
  JnpfSheetsInsertRadarEchartOperation,
  JnpfSheetsFocusEchartOperation,
} from '../commands/operations/sheet-echart.operation';

import {
  JnpfSheetsEchartMenuFactory,
  JnpfSheetsInsertBarEchartMenuFactory,
  JnpfSheetsInsertLineEchartMenuFactory,
  JnpfSheetsInsertPieEchartMenuFactory,
  JnpfSheetsInsertRadarEchartMenuFactory,
} from '../controllers/menus/sheet-echart.menu';

import univerEchartComponent from '../components/Echarts/index.vue';

import { JnpfCommandIds, JnpfUniverEchartKey } from '../utils/define';

export class JnpfSheetsEchartController extends Disposable {
  constructor(
    // @Inject(Injector) private readonly _injector: Injector,
    @ICommandService private readonly _commandService: ICommandService,
    @IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
    @Inject(ComponentManager) private readonly _componentManager: ComponentManager,
  ) {
    super();

    this._initCommands();
    this._registerComponents();
    this._initMenus();
  }

  private _initCommands(): void {
    [
      JnpfSheetsInsertedEchartOperation,
      JnpfSheetsInsertBarEchartOperation,
      JnpfSheetsInsertLineEchartOperation,
      JnpfSheetsInsertPieEchartOperation,
      JnpfSheetsFocusEchartOperation,
      JnpfSheetsInsertRadarEchartOperation,
    ].forEach(command => {
      this.disposeWithMe(this._commandService.registerCommand(command));
    });

    // 注册图表组件进来
    this.disposeWithMe(this._componentManager.register(JnpfUniverEchartKey, univerEchartComponent, { framework: 'vue3' }));
  }

  private _registerComponents(): void {
    this.disposeWithMe(this._componentManager.register('SystemSingle', SystemSingle));
    this.disposeWithMe(this._componentManager.register('ChartSingle', ChartSingle));
    this.disposeWithMe(this._componentManager.register('LineChartSingle', LineChartSingle));
    this.disposeWithMe(this._componentManager.register('PieChartSingle', PieChartSingle));
    this.disposeWithMe(this._componentManager.register('RadarChartSingle', RadarChartSingle));
  }

  private _initMenus(): void {
    this._menuManagerService.mergeMenu({
      // 主菜单
      [RibbonStartGroup.OTHERS]: {
        [JnpfCommandIds.echartOperations]: {
          order: 10,
          menuItemFactory: JnpfSheetsEchartMenuFactory,
          [JnpfSheetsInsertBarEchartOperation.id]: {
            order: 0,
            menuItemFactory: JnpfSheetsInsertBarEchartMenuFactory,
          },
          [JnpfSheetsInsertLineEchartOperation.id]: {
            order: 1,
            menuItemFactory: JnpfSheetsInsertLineEchartMenuFactory,
          },
          [JnpfSheetsInsertPieEchartOperation.id]: {
            order: 2,
            menuItemFactory: JnpfSheetsInsertPieEchartMenuFactory,
          },
          // [JnpfSheetsInsertRadarEchartOperation.id]: {
          //   order: 3,
          //   menuItemFactory: JnpfSheetsInsertRadarEchartMenuFactory,
          // },
        },
      },
    });
  }
}
